package com.game.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class MyCorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        //configuration对象
        CorsConfiguration configuration = new CorsConfiguration();
        //允许谁能访问
        configuration.addAllowedOrigin("http://localhost:8889");
        //获取cookie参数
        configuration.setAllowCredentials(true);
        //允许什么方法 Get，Post...
        configuration.addAllowedMethod("*");
        //允许的头信息
        configuration.addAllowedHeader("*");
        //拦截器,过滤资源处理
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);

        return new CorsFilter(source);
    }
}
